import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// 一级路由
import Login from '@/views/login'
import Layout from '@/views/layout'
// 二级路由
import Welcome from '@/views/welcome'
import Users from '@/views/users'
import Roles from '@/views/roles'
import Rights from '@/views/rights'
import Goods from '@/views/goods'
import Params from '@/views/params'
import Categories from '@/views/categories'
import Orders from '@/views/orders'
import Reports from '@/views/reports'
import Add from '@/views/goods/add'

Vue.use(VueRouter)

const routes = [
  // 一级路由
  { path: '/login', component: Login },
  {
    path: '/',
    component: Layout,
    redirect: '/welcome',
    children: [
      { path: 'welcome', component: Welcome },
      { path: 'users', component: Users },
      { path: 'roles', component: Roles },
      { path: 'rights', component: Rights },
      { path: 'goods', component: Goods },
      { path: 'params', component: Params },
      { path: 'categories', component: Categories },
      { path: 'orders', component: Orders },
      { path: 'reports', component: Reports },
      { path: 'add', component: Add }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // console.log(store.state.user);
  const { token } = store.state.user
  if (to.path !== '/login' && !token) return next('/login')
  next()
})

export default router
